<?php
FLEA :: loadClass('Controller_Base');

class Controller_User extends Controller_Base {
	/**
	 * user 模块，
	 *
	 * @return Controller_UserUI
	 */
	function Controller_User() {
	}

	function actionCheckLogin() {
		session_start();
		$userID	=	$_SESSION['UserID'];
		$role	=	$_SESSION['Role'];
		if($userID == "")
		{
			$userID	=	0;
		}
		if($role == '')
		{
			$role	=	0;
		}
		
		print '<data><Role>'.$role.'</Role><UserID>'.$userID.'</UserID>';
		print '</data>';
	}

	function actionClientRegister() {
		$_modelUser = & FLEA :: getSingleton('Model_User');

		$business = $_POST['Business'];
		$email = $_POST['Email'];
		$password = $_POST['Password'];

		$result = $_modelUser->clientRegister($business, $email, $password);

		$this->showResult($result);

	}

	function actionPractitionerRegister() {
		$_modelUser = & FLEA :: getSingleton('Model_User');
		$name = $_POST['Name'];
		$email = $_POST['Email'];
		$password = $_POST['Password'];

		$result = $_modelUser->practitionerRegister($name, $email, $password);
		switch ($result) {
			case 0 :
				print "The email is used,try other";
				break;
			case 1 :
				print 'Successfully register practitioner';
				break;
		}
	}

	function actionUserLogin() {
		$_modelUser = & FLEA :: getSingleton('Model_User');
		$email = $_POST['Email'];
		$password = $_POST['Password'];

		$user = $_modelUser->userLogin($email, $password);

		if ($user['UserID'] == '') {
			$role = 0;
		} else
			if ($user['Password'] != $password) {
				$role = -1;
			} else
				if ($user['IPManager'] == 1) {
					$role = 5;
				} else {
					if ($user['ClientID'] != '') {
						$role = 1;
					} else
						if ($user['InventorID'] != '') {
							/**
							 * Ok! Go inventor screen.....
							 */
							$role = 2;
						} else
							if ($user['FirmID'] != '') {
								$role = 3;
							} else
								if ($user['PractitionerID'] != '') {
									$role = 4;
								}
				}

		print '<result><role>' . $role . '</role><UserID>' . $user['UserID'] . '</UserID></result>';
	}

	function actionGetUser() {
		$userID = $_POST['UserID'];
		$_modelUser = & FLEA :: getSingleton('Model_User');
		$user = $_modelUser->findUser($userID);
		if (is_array($user)) {
			$this->purse($user, 'user', 0);
		}
	}

	function actionChangePassword() {
		$_modelUser = & FLEA :: getSingleton('Model_User');

		$result = $_modelUser->changePassword($_POST['UserID'], $_POST['OldPassword'], $_POST['NewPassword']);
		$this->showResult($result);
	}

	function actionEditPersonalInformation() {
		$user = $_POST;
		if ($user['UserID'] > 0) {
			$_modelUser = & FLEA :: getSingleton('Model_User');
			$result = $_modelUser->save($user);
			$this->showResult($result);
			exit (0);
		}
		$this->showResult(-2);
	}

	function actionEditBilling() {
		$client = $_POST;
		$_modelClient = & FLEA :: getSingleton('Model_Client');
		$result = $_modelClient->save($client);
		$this->showResult($result);
	}

	function actionAddInventor() {
		$_modelInventor = & FLEA :: getSingleton('Model_Inventor');
		$email = $_POST['Email'];
		$demo = $_POST['Message'];
		$firstName = $_POST['FirstName'];
		$lastName = $_POST['LastName'];
		$clientID = $_POST['ClientID'];
		$result = $_modelInventor->addInventor($firstName, $lastName, $email, $demo, $clientID);

		$this->showResult($result);
	}

	function actionEditFirm() {
		$firm = $_POST;
		$_modelFirm = & FLEA :: getSingleton('Model_Firm');
		$result = $_modelFirm->save($firm);
		$this->showResult($result);
	}

	function actionAddClient() {
		$firmID = $_POST['FirmID'];

		$_modelUser = & FLEA :: getSingleton('Model_User');
		$result = $_modelUser->clientRegister($_POST['Business'], $_POST['Email'], $_POST['Email'], htmlspecialchars($_POST['Message']), $_POST['FirstName'], $_POST['LastName'], $firmID);

		$this->showResult($result);
	}

	function actionAddPractitioner() {
		$_modelFirm = & FLEA :: getSingleton('Model_Firm');

		$practitioner['FirmID'] = $_POST['FirmID'];
		$practitioner['BAttorney'] = $_POST['BAttorney'];
		$practitioner['PatentBar'] = $_POST['PatentBar'];
		$practitioner['Editor'] = $_POST['Editor'];
		$practitioner['Draftsman'] = $_POST['Draftsman'];

		$user['FirstName'] = $_POST['FirstName'];
		$user['LastName'] = $_POST['LastName'];
		$user['Email'] = $_POST['Email'];

		$message = $_POST['Message'];

		$result = $_modelFirm->addPractitioner($user, $practitioner, $message);

		$this->showResult($result);
	}

	function actionGetPractitioners() {
		$_modelPractitioner = & FLEA :: getSingleton('Model_Practitioner');
		$practitioners = $_modelPractitioner->findAllPractitioners($_POST['FirmID']);
		if (is_array($practitioners)) {
			$this->purse($practitioners, 'practitioners', 0);
		}
	}

	function actionGetInventors() {
		$_modelInventor = & FLEA :: getSingleton('Model_Inventor');
		$inventors = $_modelInventor->findAllInventors($_POST['ClientID']);
		if (is_array($inventors)) {
			$this->purse($inventors, 'inventors', 0);
		}
	}

	function actionGetClients() {
		$_modelClient = & FLEA :: getSingleton('Model_Client');
		$clients = $_modelClient->findAllClients();
		if (is_array($clients)) {
			$this->purse($clients, 'clients', 0);
		}
	}

	function actionGetClient() {
		$_modelClient = & FLEA :: getSingleton('Model_Client');
		$client = $_modelClient->findClient($_POST['ClientID']);
		if (is_array($client)) {
			$this->purse($client, 'client', 0);
		}
	}

	function actionGetFirms() {
		$_modelFirm = & FLEA :: getSingleton('Model_Firm');
		$firms = $_modelFirm->findAllFirms();
		if (is_array($firms)) {
			$this->purse($firms, 'firms', 0);
		}
	}

	function actionGetFirmsWork() {
		$clientID = $_POST['ClientID'];
		$clientID = $clientID == '' ? 0 : $clientID;
		$_modelFirm = & FLEA :: getSingleton('Model_Firm');
		$firms = $_modelFirm->findAllFirmsWork($clientID);
		if (is_array($firms)) {
			$this->purse($firms, 'firms', 0);
		}
	}

	function actionGetFirm() {
		$_modelFirm = & FLEA :: getSingleton('Model_Firm');
		$firm = $_modelFirm->findFirm($_POST['FirmID']);
		if (is_array($firm)) {
			$this->purse($firm, 'firm', 0);
		}
	}

	function actionDeleteInventor() {
		$_modelInventor = & FLEA :: getSingleton('Model_Inventor');
		$inventorID = $_POST['InventorID'];
		$result = $_modelInventor->deleteInventor($inventorID);
		$this->showResult($result);
	}
	function actionDeletePractitioner() {
		$_modelPractitioner = & FLEA :: getSingleton('Model_Practitioner');
		$practitionerID = $_POST['PractitionerID'];
		$result = $_modelPractitioner->deletePractitioner($practitionerID);
		$this->showResult($result);
	}

	function actionUpdatePractitioner() {
		$_modelPractitioner = & FLEA :: getSingleton('Model_Practitioner');
		$practitioner = $_POST;
		$result = $_modelPractitioner->save($practitioner);
		$this->showResult($result);
	}

	function actionUpdateFirms() {
		$_modelFirm = & FLEA :: getSingleton('Model_Firm');

		$result = $_modelFirm->updateFirms($_POST['Firms'], $_POST['Type']);
		$this->showResult($result);
	}

	function actionUpdateClient() {
		$_modelClient = & FLEA :: getSingleton('Model_Client');
		$clientID = $_POST['ClientID'];
		$clientTem = $_modelClient->findClient($clientID);

		$client['BAuction'] = $clientTem['BAuction'] == 1 ? 0 : 1;
		$client['ClientID'] = $clientID;
		$result = $_modelClient->save($client);

		$this->showResult($result);
	}

	function actionGetPractitioner() {
		$_modelPractitioner = & FLEA :: getSingleton('Model_Practitioner');
		$practitionerID = $_POST['PractitionerID'];
		$practitioner = $_modelPractitioner->findPractitioner($practitionerID);
		if (is_array($practitioner)) {
			$this->purse($practitioner, 'practitioner', 0);
		}
	}

	function actionLogOut() {
		session_start();
		//  这种方法是将原来注册的某个变量销毁
		unset ($_SESSION['UserID']);
		//  这种方法是销毁整个 Session 文件
		session_destroy();

		$this->showResult(1);
	}

	function actionDeleteClient() {
		$isLogin = $this->checkLogin();
		if ($isLogin == 5) {
			$userID = $_POST['UserID'];
			$result = $this->_modelUser->removeByPkv($userID);
		} else {
			$result = -2;
		}

		if ($isLogin == 5) {
			$_modelClient = & FLEA :: getSingleton('Model_Client');
			$clients = $_modelClient->findAllClients();
		}

		$this->viewdata['result'] = $result;
		$this->function = 'ClientsList';
		$this->viewdata['clients'] = $clients;
		$this->module = "Client";
		$this->show();
	}

	function actionDeleteFirm() {
		$_modelFirm = & FLEA :: getSingleton('Model_Firm');
		$firmID = $_POST['FirmID'];
		$result = $_modelFirm->deleteFirm($firmID);

		$this->showResult($result);
	}

	function actionInviteeFirm() {
		$email = $_POST['Email'];
		$message = $_POST['Message'];
		require ("Extra/Mail.php");
		set_time_limit(100);

		$SmtpEmailTo = $email;

		$mailsubject = "[IP Manager] ------";
		$mailHeader = "Content-Type:text/HTML ";
		if ($message == "") {
			$mailBody = "<div style='font-family: \"Times New Roman\", Times, serif;color:#000000;font-size:14px;
							font-weight:800;'>Welcome to IP Manager.<br>
							We invite you to participate in our bidding . First , may you need to register at IP Manager System .</div>";
		} else {
			$mailBody = "<div style='font-family: \"Times New Roman\", Times, serif;color:#000000;font-size:14px;
							font-weight:800;'>" . $message . "</div>";
		}
		$smtp = new smtp(SmtpServer, Smtpserverport, true, SmtpUser, SmtpPass);
		$smtp->debug = true;

		$re = $smtp->sendmail($SmtpEmailTo, SmtpUserMail, $mailsubject, $mailBody, "", "", "", $mailHeader);
		$this->showResult($re);
	}

	function actionGetCopyRight() {
		$_modelCopyRight = & FLEA :: getSingleton('Model_CopyRight');
		$clientID = $_POST['ClientID'];
		$clientID = $clientID == '' ? 0 : $clientID;

		$copy = $_modelCopyRight->findByField('ClientID', $clientID);
		if (is_array($copy)) {
			$this->purse($copy, 'copy', 0);
		}
	}

	function actionSetCopyRight() {
		$_modelCopyRight = & FLEA :: getSingleton('Model_CopyRight');

		$copy = $_POST;

		$result = $_modelCopyRight->saveCopy($copy);
		$this->showResult($result > 0 ? 1 : 0);
	}

	function actionGetFirmMatters() {
		$firmID = $_POST['FirmID'];

		$_modelActionEvent = & FLEA :: getSingleton("Model_ActionEvent");
		$clients = $_modelActionEvent->findFirmMatters($firmID);

		$this->purse($clients, 'clients', 0);
	}
	
	function actionGetMatters() 
	{
		$firmID = $_POST['FirmID'];

		$_modelMatter = & FLEA :: getSingleton("Model_Matter");
		$matters = $_modelMatter->findFirmMatters($firmID);

		$this->purse($matters, 'matters', 0);
	}

	function actionAddMatter() 
	{
		$title		=	$_POST['Title'];
		$clientEmail = $_POST['ClientEmail'];
		$message	=	$_POST['Message'];

		$matter['ClientID'] = $_POST['ClientID'];
		$matter['FirmID'] = $_POST['FirmID'];
		$matter['Type'] = $_POST['Type'];
		$matter['Message'] = $_POST['Message'];

		switch($matter['Type'])
		{
			case 0:
				$_modelInvention	=&	FLEA::getSingleton("Model_Invention");
				$invention['ClientID']	=	$matter['ClientID'];
				$invention['FirmID']	=	$matter['FirmID'];
				$invention['Title']		=	$title;
				$invention['Type']		=	-1;
				$invention['BConfirm']	=	1;
				$result1		=	$_modelInvention->create($invention);
				break;
			case 1:
				$_modelTradeMark	=&	FLEA::getSingleton("Model_TradeMark");
				
				$trade['ClientID']	=	$matter['ClientID'];
				$trade['FirmID']	=	$matter['FirmID'];
				$trade['TradeMark']	=	$title;
				$trade['BConfirm']	=	1;
				
				$result1	=	$_modelTradeMark->create($trade);
				break;
			case 2:
				$_modelCopyright	=&	FLEA::getSingleton("Model_Copyright");
				
				$copyright['ClientID']	=	$matter['ClientID'];
				$copyright['FirmID']	=	$matter['FirmID'];
				$copyright['Title']		=	$title;
				$trade['BConfirm']		=	1;
				$result1	=	$_modelCopyright->create($copyright);
				break;
		}
		
		$_modelMatter = & FLEA :: getSingleton("Model_Matter");
		$result = $_modelMatter->create($matter);

		$SmtpEmailTo = $clientEmail;

		$mailsubject = "[IP Manager] ------";
		$mailHeader = "Content-Type:text/HTML ";
		
		$re	=	$this->sendMail($SmtpEmailTo,$mailsubject,$message,$mailHeader);

		$this->showResult($result > 0 ? 1 : 0);
	}
}
?>